package com.patent.common;

import com.patent.domain.vo.ExportParaVo;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ExcelUtils {

	//导出文件 配置的文件夹
	private static final String PREFIX = "jxl/";

	private static String SUFFIX = ".xlsx" ;
	//文件夹路径
	private static String PATH = null ;
	static{
		PATH = ExcelUtils.class.getClassLoader().getResource("").getPath() ;
	}

	public static String exportExcel(ExportParaVo paravo, HttpServletRequest request, HttpServletResponse response) {
		String fileName = "";
		//导出文件配置名
		String fileKey = paravo.getFileKey() ;
		String realPath = "";
		String defaultFileName = "";
		//文件绝对路径
		realPath = PATH + PREFIX + fileKey + SUFFIX ;
		//导出文件默认名字
		defaultFileName = paravo.getDefaultFileName() + SUFFIX;

		//要导出的数据
		Object data = paravo.getData() ;
		//得到模板转换对象
		XLSTransformer transformer = new XLSTransformer();
		//servlet上下文对象

		InputStream inputStream = null ;
		try {
			//得到模板流
			inputStream = new FileInputStream(realPath) ;
			//参数对象
			Map beans = new HashMap();
			//模板里定义了items,会遍历 items，和模板对应
			beans.put("items", data);
			//开始转换，会把beans里的items对象，遍历填充到模板，生成一个文件流
			Workbook hssfworkbook = transformer.transformXLS(inputStream, beans);
			//设置response导出excel的参数
			//defaultFileName= new String(defaultFileName.getBytes("GBK"),"ISO-8859-1") ;
			//response.setHeader("Content-Disposition", "attachment;filename="+defaultFileName );
			//response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			//把导出的excel文件流，写入response
			//hssfworkbook.write(response.getOutputStream());
			//response.getOutputStream().flush();

			FileOutputStream fileOut = null;
			String plath = "";
			try {
				Date date=new Date();
				long times=date.getTime();//当前毫秒数
				File directory = new File("");//参数为空
				File file = new File(directory.getCanonicalPath() + File.separator + "chem_temp");
				file.mkdirs();
				plath =  directory.getCanonicalPath() +File.separator+"chem_temp"+File.separator+"tempOrderExcel_" + String.valueOf(times) + ".xlsx";
				System.out.println("path:" + plath);
				//windows下
				if("\\".equals(File.separator)){
					plath  = plath.substring(0,plath.length());
					plath = plath.replace("/", "\\");
				}
				//linux下
				if("/".equals(File.separator)){
					plath  = plath.substring(0,plath.length());
					plath = plath.replace("\\", "/");
				}

				fileOut = new FileOutputStream(plath);
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
			}
			hssfworkbook.write(fileOut);
			fileOut.close();

			File f = new File(plath);
			InputStream input = new FileInputStream(f);
			byte[] byt = new byte[input.available()];
			input.read(byt);
			fileName = QiNiuUtil.uploadFile(byt);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e) ;
		}finally{
			try {
				if(inputStream!=null){
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				throw new RuntimeException(e) ;
			}
		}
		return fileName;
	}
}
